<?php
class categoriaAfiliado extends Controller 
{
  public function __construct()
  {
		parent::Controller();
		$this->load->database();
		$this->load->model('mcategoriaafiliado');
  }
  
  public function index(){
   
     $this->load->view('main', array('content_page' => 'ca_list','menutop' => 0));
  }
   
  public function data() 
  {
		$page = $this->input->post('page') ? $this->input->post('page') : 1; // Pagina Actual o 1 por default
		$sortname = 'IdPais';
		$sortorder = 'asc'; // Orden (asc|desc)
		$limit = $this->input->post('rows') ? $this->input->post('rows') : 10;
		$start = $limit * $page - $limit;
		if($start < 0) $start = 0;

		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
		header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
		header("Cache-Control: no-cache, must-revalidate" );
		header("Pragma: no-cache" );

		$records = $this->mcategoriaafiliado->LoadCategoria();

		$totalRecords = $this->mcategoriaafiliado->GetCount();

		$totalPages = ceil($totalRecords/$limit);
		if ($page > $totalPages) $page = $totalPages;

		$json_output = array(
		'page'		=> $page,
		'total'		=> $totalPages,
		'records'	=> $totalRecords,
		'rows'		=> array()
		);

		if($records != false) 
		{
			foreach($records as $pais) {
				$arr = $json_output['rows'][] = array(
				       'IdCategoria'        => $pais->getIdCategoria(),
				       'NombreCAfiliado'    => $pais->getNombreCategoria());
			}
		}

		echo json_encode($json_output);
   }
   
   public function add(){
       $this->load->view('main', array('content_page' => 'ca_add','menutop' => 0));
   }
   
   public function delete($id){
  
   	  $where = array('idCategoria' => $id);
   	  
   	  if($this->mcategoriaafiliado->delRegistro($where) == true){
   	     redirect('categoriaAfiliado');
   	  }else{
   	  
   	  }
   	  
   	
   }
   
   public function edit($id){
   	   
   	    $where = array('idCategoria'=>$id);
   	
   	      if ($registro = $this->mcategoriaafiliado->loadCategoriaId($where)) {
   	      	    $this->load->view('main', array('content_page' => 'ca_edit','menutop' => 0,'registro'=>$registro));  
   	      }
   	   
   }
   
   public function save(){
      $nombre = $this->input->post('name');
      $id = @$this->input->post('id');
      
      if ($id > 0) {
            
      	   $where = array('idCategoria'=>$this->input->post('id'));
      	   $data  = array('descriccion'=>$nombre);
      	   
           if ($this->mcategoriaafiliado->editRegistro($where,$data) == true) {
      	     redirect('categoriaAfiliado');
          }else{
        
          }
      	   
      }else{

      	  if ($this->mcategoriaafiliado->addRegistro(array('descriccion'=>$nombre)) == true) {
      	     redirect('categoriaAfiliado');
          }else{
        
          }
     
      }
      
    
      
      
      
   }


}